<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>TopM.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="TopM code in Java">
<meta NAME="TITLE" CONTENT="TopM code in Java">
<meta NAME="KEYWORDS" CONTENT="TopM,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>TopM.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "TopM.java">TopM.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac TopM.java</span>
<span class="comment"> *  Execution:    java TopM M &lt; input.txt</span>
<span class="comment"> *  Dependencies: MinPQ.java Transaction.java StdIn.java StdOut.java</span>
<span class="comment"> *  Data files:   </span><span class="url">http://algs4.cs.princeton.edu/24pq/tinyBatch.txt</span>
<span class="comment"> * </span>
<span class="comment"> *  Given an integer M from the command line and an input stream where</span>
<span class="comment"> *  each line contains a String and a long value, this MinPQ client</span>
<span class="comment"> *  prints the M lines whose numbers are the highest.</span>
<span class="comment"> * </span>
<span class="comment"> *  % java TopM 5 &lt; tinyBatch.txt </span>
<span class="comment"> *  Thompson    2/27/2000  4747.08</span>
<span class="comment"> *  vonNeumann  2/12/1994  4732.35</span>
<span class="comment"> *  vonNeumann  1/11/1999  4409.74</span>
<span class="comment"> *  Hoare       8/18/1992  4381.21</span>
<span class="comment"> *  vonNeumann  3/26/2002  4121.85</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">TopM</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class provides a client that reads a sequence of</span>
<span class="comment"> *  transactions from standard input and prints the </span><span class="keyword">&lt;em&gt;</span><span class="comment">M</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> largest ones</span>
<span class="comment"> *  to standard output. This implementation uses a {</span><span class="type">@link</span><span class="comment"> MinPQ} of size</span>
<span class="comment"> *  at most </span><span class="keyword">&lt;em&gt;</span><span class="comment">M</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> + 1 to identify the </span><span class="keyword">&lt;em&gt;</span><span class="comment">M</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> largest transactions</span>
<span class="comment"> *  and a {</span><span class="type">@link</span><span class="comment"> Stack} to output them in the proper order.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation, see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/24pq"</span><span class="keyword">&gt;</span><span class="comment">Section 2.4</span><span class="keyword">&lt;/a&gt;</span>
<span class="comment"> *  of </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">TopM</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal">   </span>

<span class="normal">    </span><span class="comment">// This class should not be instantiated.</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="function">TopM</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  Reads a sequence of transactions from standard input; takes a</span>
<span class="comment">     *  command-line integer M; prints to standard output the M largest</span>
<span class="comment">     *  transactions in descending order.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> M </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span><span class="normal"> </span>
<span class="normal">        </span><span class="usertype">MinPQ&lt;Transaction&gt;</span><span class="normal"> pq </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> MinPQ</span><span class="symbol">&lt;</span><span class="normal">Transaction</span><span class="symbol">&gt;(</span><span class="normal">M</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">);</span><span class="normal"> </span>

<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">StdIn</span><span class="symbol">.</span><span class="function">hasNextLine</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="comment">// Create an entry from the next line and put on the PQ. </span>
<span class="normal">            </span><span class="usertype">String</span><span class="normal"> line </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readLine</span><span class="symbol">();</span>
<span class="normal">            </span><span class="usertype">Transaction</span><span class="normal"> transaction </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Transaction</span><span class="symbol">(</span><span class="normal">line</span><span class="symbol">);</span>
<span class="normal">            pq</span><span class="symbol">.</span><span class="function">insert</span><span class="symbol">(</span><span class="normal">transaction</span><span class="symbol">);</span><span class="normal"> </span>

<span class="normal">            </span><span class="comment">// remove minimum if M+1 entries on the PQ</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">pq</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> M</span><span class="symbol">)</span><span class="normal"> </span>
<span class="normal">                pq</span><span class="symbol">.</span><span class="function">delMin</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span><span class="normal">   </span><span class="comment">// top M entries are on the PQ</span>

<span class="normal">        </span><span class="comment">// print entries on PQ in reverse order</span>
<span class="normal">        </span><span class="usertype">Stack&lt;Transaction&gt;</span><span class="normal"> stack </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Stack</span><span class="symbol">&lt;</span><span class="normal">Transaction</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Transaction</span><span class="normal"> transaction </span><span class="symbol">:</span><span class="normal"> pq</span><span class="symbol">)</span>
<span class="normal">            stack</span><span class="symbol">.</span><span class="function">push</span><span class="symbol">(</span><span class="normal">transaction</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Transaction</span><span class="normal"> transaction </span><span class="symbol">:</span><span class="normal"> stack</span><span class="symbol">)</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">transaction</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span><span class="normal"> </span>
<span class="cbracket">}</span><span class="normal"> </span>


<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
